package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignonServlet extends HttpServlet {

    private static final String SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";

    private String username;
    private String password;

    private Account account;
    private List<Product> myList;
    boolean authenticated;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

//        VerificationCode verificationCode;

        username = request.getParameter("username");
        password = request.getParameter("password");
        AccountService service = new AccountService();

        account = null;
        account = service.getAccount(username, password);



        //获得输入的验证码值
        String value1=request.getParameter("vCode");
        /*获取图片的值*/
        String value2=(String)session.getAttribute("checkcode");
        System.out.println("value2:" + value2);
        Boolean isSame = false;
        /*对比两个值（字母不区分大小写）*/
        if(value2.equalsIgnoreCase(value1)){
            isSame = true;
        }
        if (account == null || !isSame) {
            if(!isSame){
                session.setAttribute("message", "Invalid Verification Code.   Signon failed.");
            }else{
                session.setAttribute("message", "Invalid username or password.  Signon failed.");
            }


            //clear

            account = new Account();
            session.setAttribute("account", account);

            myList = null;
            session.setAttribute("myList", myList);

            authenticated = false;
            session.setAttribute("authenticated", authenticated);



            Account account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + SIGNON;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(SIGNON).forward(request,response);
        } else {
            //登录成功
            //初始化购物车
            Cart cart = new Cart();
            String userid = account.getUsername();
            List<CartItem> cartItemList = cart.getCartItemListByUserid(userid);
//            for (int i = 0; i < cartItemList.size(); i++) {
//                System.out.println(cartItemList.get(i).getItem().getItemId());
//            }
            for (int i = 0; i < cartItemList.size(); i++) {
                CartItem cartItem = cartItemList.get(i);
                cart.addItem1(cartItem);
            }
            session.setAttribute("cart", cart);


            //放入username备用
            session.setAttribute("username", account.getUsername());

            account.setPassword(null);

            CatalogService catalogService = new CatalogService();
            myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());

            boolean authenticated = true;


            //bean用其中的bean中属性散装放入session中
//            s.setAttribute("accountBean", this);
            session.setAttribute("account", account);
            session.setAttribute("myList", myList);
            session.setAttribute("authenticated", authenticated);
            session.setAttribute("message", null);

            Account account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + MAIN;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(MAIN).forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
